package org.example.diet_compass.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.annotation.Resource;
import org.example.diet_compass.domain.CategoryTops;
import org.example.diet_compass.domain.Goods;
import org.example.diet_compass.domain.Scores;
import org.example.diet_compass.dto.ScoreRecord;
import org.example.diet_compass.dto.TopCategoryItem;
import org.example.diet_compass.resp.Result;
import org.example.diet_compass.resp.ResultCodeEnum;
import org.example.diet_compass.service.GoodsService;
import org.example.diet_compass.service.ScoresService;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

/**
 * @author Archer_hui
 * @version 1.0
 * @Date 2025/3/2 9:47
 */
@Tag(name = "关于商品的控制类", description = "主要包括了根据菜品id获取菜品，根据菜品名字模糊查询菜品")
@RestController
@RequestMapping("/goods")
public class GoodsController {
    @Resource
    private GoodsService goodsService;

    @Resource
    private ScoresService scoresService;


    /**
     * 根据id获取商品
     *
     * @param id
     * @return
     */
    @GetMapping("/get-by-id")
    @Operation(summary = "根据商品id获取商品")
    public Result<Goods> getGoodById(@RequestParam("id") String id) {
        //根据id获取商品
        return Result.build(goodsService.getGoodById(id),ResultCodeEnum.SUCCESS);
    }

    @GetMapping("/pages")
    @Operation(summary = "分页获取所有菜品")
    public Result<IPage<Goods>> getHomeGoodsByPage(@RequestParam(value = "currentPage", defaultValue = "1") int currentPage,
                                                   @RequestParam(value = "pageSize", defaultValue = "5") int pageSize
   ) {

        return Result.build(goodsService.getGoodsByPageAndKeyword(currentPage, pageSize), ResultCodeEnum.SUCCESS);
    }

    @GetMapping("/search")
    @Operation(summary = "根据菜品名搜索")
    public Result<List<Goods>> getGoodsByName(@RequestParam (value = "keyword",defaultValue = "")String keyword) {
        QueryWrapper<Goods> queryWrapper = new QueryWrapper<>();
        queryWrapper.lambda().like(keyword != null, Goods::getName, keyword);
        return Result.build( goodsService.list(queryWrapper), ResultCodeEnum.SUCCESS);
    }

    /**
     *      根据用户id，商品id来更新或者创建评分
     * @param scoreRecord 评分信息获取
     * @return  包装的封装类
     */
    @PostMapping("/score")
    @Transactional
    @Operation(summary = "用于修改或者新建菜品评分")
    public Result<Void> updateDietsScore(@RequestBody ScoreRecord scoreRecord) {

        String goodId = scoreRecord.getGoodId();
        int userId = scoreRecord.getUserId();
        int score = scoreRecord.getScore();

        Scores getScore = scoresService.selectByGoodIdAndUserId(goodId, userId);
        try {

            if (getScore == null) {
                getScore = new Scores();
                getScore.setScore(score);
                getScore.setGoodsId(goodId);
                getScore.setUserId(userId);
            }
            getScore.setScore(score);
            scoresService.saveOrUpdate(getScore);
            goodsService.updateScore(goodId);
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
        return Result.build(null, ResultCodeEnum.SUCCESS);
    }
    @GetMapping("/category")
    @Operation(summary = "用来实现商品一二级查询的方法")
    public Result<List<TopCategoryItem>> getGoodsByCategory() {
        return Result.build(goodsService.getGoodsByCategory(), ResultCodeEnum.SUCCESS);
    }

    @GetMapping("/get-score-record")
    @Operation(summary = "获取特定用户对特定商品的评分记录的api")
    public Result<Integer> getScoreRecord(@RequestParam("goodId") String goodId,
                                          @RequestParam("userId") Integer userId) {
        Integer score = scoresService.getScoreByGoodIdAndUserId(goodId, userId);
        return Result.build(score, ResultCodeEnum.SUCCESS);
    }

    @GetMapping("/get-good-score-num")
    @Operation(summary = "获取一个商品的评分人数api")
    public Result<Integer> getGoodScores(@RequestParam("goodId") String goodsId) {
       return Result.build( scoresService.getGoodScoreNums(goodsId),ResultCodeEnum.SUCCESS);
    }


}
